package com.fanfanwang.ffw.app.widget.expandlist;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.fanfanwang.ffw.app.AppConstant;
import com.fanfanwang.ffw.app.R;
import com.fanfanwang.ffw.app.adapter.TextAdapter;
import com.fanfanwang.ffw.app.bean.CityListBean;
import com.fanfanwang.ffw.app.bean.PianQuBean;

public class ViewMiddle extends LinearLayout implements ViewBaseAction {

	private ListView regionListView;
	private ListView plateListView;
	private ArrayList<CityListBean> groups = new ArrayList<CityListBean>();
	private LinkedList<PianQuBean> childrenItem = new LinkedList<PianQuBean>();
	private LinkedList<String> childrenString=new LinkedList<String>();
	private SparseArray<LinkedList<PianQuBean>> children = new SparseArray<LinkedList<PianQuBean>>();
	private TextAdapter plateListViewAdapter;
	private TextAdapter earaListViewAdapter;
	private OnSelectListener mOnSelectListener;
	private int tEaraPosition = 0;
	private int tBlockPosition = 0;
	private String showString = "不限";
	private String cityid="";
	public ViewMiddle(Context context,ArrayList<CityListBean> groups) {
		super(context);
		init(context);
		this.groups.clear();
		if(groups!=null)
		{
			this.groups.addAll(groups);
		}
	}

	public ViewMiddle(Context context) {
		super(context);
		init(context);
	}

	public ViewMiddle(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	/*	public void updateShowText(String showArea, String showBlock) {
		if (showArea == null || showBlock == null) {
			return;
		}
		for (int i = 0; i < groups.size(); i++) {
			if (groups.get(i).equals(showArea)) {
				earaListViewAdapter.setSelectedPosition(i);
				childrenItem.clear();
				childrenString.clear();
				if (i < children.size()) {
					childrenItem.addAll(children.get(i));

					tEaraPosition = i;
					break;
				}
			}
		}
		for (int j = 0; j < childrenItem.size(); j++) {
			if (childrenItem.get(j).pianqu.replace("不限", "").equals(showBlock.trim())) {
				plateListViewAdapter.setSelectedPosition(j);
				tBlockPosition = j;
				break;
			}
		}
		setDefaultSelect();
	}*/
	//更新数据
	public void resetData(ArrayList<CityListBean> groups){
		children.clear();
		List<String> area=new ArrayList<String>();
		for(int i=0;i<groups.size();i++)
		{
			area.add(AppConstant.URLDecoderString(groups.get(i).title));
			LinkedList<PianQuBean> tItem = new LinkedList<PianQuBean>();
			int secondSize=groups.get(i).data.size();
			for(int j=0;j<secondSize;j++){
				PianQuBean bean=new PianQuBean();
				bean.pianqu=AppConstant.URLDecoderString(groups.get(i).data.get(j).title);
				bean.cityid=groups.get(i).data.get(j).id;
				tItem.add(bean);
			}
			children.put(i, tItem);
		}
		earaListViewAdapter.resetData(area);
		earaListViewAdapter.notifyDataSetChanged();

	}
	private void init(Context context) {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.view_region, this, true);
		regionListView = (ListView) findViewById(R.id.listView);
		plateListView = (ListView) findViewById(R.id.listView2);
		setBackgroundDrawable(getResources().getDrawable(
				R.drawable.choosearea_bg_left));

		//TODO 处理数据
		List<String> area=new ArrayList<String>();
		area.add("桥东区");
		area.add("裕华区");
		area.add("长安区");
		area.add("桥西区");
		area.add("新华区");
		/*for(int i=0;i<area.size();i++){
//			area.add(i+"行");
			LinkedList<String> tItem = new LinkedList<String>();
			for(int j=0;j<10;j++){

				tItem.add("停车位"+j+"");

			}
			children.put(i, tItem);
		}*/


		earaListViewAdapter = new TextAdapter(context, area,
				R.drawable.choose_item_selected,
				R.drawable.choose_eara_item_selector);
		earaListViewAdapter.setTextSize(17);
		earaListViewAdapter.setSelectedPositionNoNotify(tEaraPosition);
		regionListView.setAdapter(earaListViewAdapter);
		earaListViewAdapter
		.setOnItemClickListener(new TextAdapter.OnItemClickListener() {

			@Override
			public void onItemClick(View view, int position) {
				if (position < children.size()) {
					childrenItem.clear();
					childrenItem.addAll(children.get(position));
					childrenString.clear();
					if(position<childrenItem.size())
					{
						if(childrenItem.get(position)!=null)
						{
							childrenString.add(childrenItem.get(position).pianqu);
						}
					}
					plateListViewAdapter.notifyDataSetChanged();
				}
			}
		});
		if (tEaraPosition < children.size())
			childrenItem.addAll(children.get(tEaraPosition));

		for(int i=0;i<childrenItem.size();i++)
		{
			childrenString.add(childrenItem.get(i).pianqu);
		}

		plateListViewAdapter = new TextAdapter(context, childrenString,
				R.drawable.choose_item_right,
				R.drawable.choose_plate_item_selector);
		plateListViewAdapter.setTextSize(15);
		plateListViewAdapter.setSelectedPositionNoNotify(tBlockPosition);
		plateListView.setAdapter(plateListViewAdapter);
		plateListViewAdapter
		.setOnItemClickListener(new TextAdapter.OnItemClickListener() {

			@Override
			public void onItemClick(View view, final int position) {

				showString = childrenItem.get(position).pianqu;
				cityid=childrenItem.get(position).cityid;
				if (mOnSelectListener != null) {

					mOnSelectListener.getValue(cityid,showString);
				}

			}
		});
		if (tBlockPosition < childrenItem.size())
			showString = childrenItem.get(tBlockPosition).pianqu;
		if (showString.contains("不限")) {
			showString = showString.replace("不限", "");
		}
		setDefaultSelect();

	}

	public void setDefaultSelect() {
		regionListView.setSelection(tEaraPosition);
		plateListView.setSelection(tBlockPosition);
	}

	public String getShowText() {
		return showString;
	}

	public void setOnSelectListener(OnSelectListener onSelectListener) {
		mOnSelectListener = onSelectListener;
	}

	public interface OnSelectListener {
		public void getValue(String cityid,String showText);
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub

	}

	@Override
	public void show() {
		// TODO Auto-generated method stub

	}
}
